﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.AttributedModel;

namespace System.ComponentModel.Composition.Factories
{
    internal static partial class ContainerFactory
    {
        public static CompositionContainer CreateWithDefaultAttributedCatalog()
        {
            var catalog = CatalogFactory.CreateDefaultAttributed();
            var container = new CompositionContainer(catalog);
            return container;
        }

        public static CompositionContainer CreateWithAttributedCatalogForTypes(params Type[] types)
        {
            var catalog = CatalogFactory.CreateAttributedForTypes(types);
            return new CompositionContainer(catalog);
        }

        public static CompositionContainer Create()
        {
            return Create((ComposablePart[])null);
        }

        public static CompositionContainer Create(CompositionContainer parent)
        {
            return new CompositionContainer(parent);
        }

        public static CompositionContainer Create(params object[] parts)
        {
            var container = new CompositionContainer();
            var partsArray = new ComposablePart[parts.Length];

            for (int i = 0; i < parts.Length; i++)
            {
                partsArray[i] = PartFactory.CreateAttributed(parts[i]);
            }

            return Create(partsArray);
        }

        public static CompositionContainer Create(params ComposablePart[] parts)
        {
            return Create((CompositionContainer)null, parts);
        }

        public static CompositionContainer Create(CompositionContainer parent, params ComposablePart[] parts)
        {
            var container = new CompositionContainer(parent);

            if (parts != null)
            {
                foreach (ComposablePart part in parts)
                {
                    container.AddPart(part);
                }
            }

            return container;
        }

        public static CompositionContainer Create(params MicroExport[] exports)
        {
            var part = PartFactory.CreateExporter(exports);

            return Create(part);
        }

        public static CompositionContainer Create(CompositionContainer parent, params MicroExport[] exports)
        {
            var part = PartFactory.CreateExporter(exports);

            return Create(parent, part);
        }

     
        public static CompositionContainer CreateDisposable(Action<bool> disposeCallback)
        {
            return new DisposableCompositionContainer(disposeCallback);
        }

        public static CompositionContainer CreateWithCreatePartCallback(Func<object, ComposablePart> createPartCallback)
        {
            return new CreatePartCompositionContainer(createPartCallback);
        }
    }
}
